<template>
	<view class="bg min100" style="padding-bottom: 118rpx">
		<view class="status_bar"></view>

		<!-- 		<view class="back">
			<image src="/static/back.png" @click="handleBack"></image>
		</view> -->

		<view class="show-price-box">
			<!-- <view class="show-price-header">
				<text>商品列表</text>
				<image src="../../static/close1.png" @tap="showMore = false"></image>
			</view> -->
			<view class="sort-type">
				<view @click="moreListSortCheck('time_order')" class="flex mr-40 sort"
					:class="{active: query.time_order}">
					<view class="mr-10 size-28">时间</view>
					<image src="../../static/sort.png" v-if="query.time_order === ''" />
					<image src="../../static/sort-asc.png" v-if="query.time_order === 1" />
					<image src="../../static/sort-desc.png" v-if="query.time_order === 2" />
				</view>
				<view @click="moreListSortCheck('price_order')" class="flex mr-40 sort"
					:class="{active: query.price_order}">
					<view class="mr-10 size-28">价格</view>
					<image src="../../static/sort.png" v-if="query.price_order === ''" />
					<image src="../../static/sort-asc.png" v-if="query.price_order === 1" />
					<image src="../../static/sort-desc.png" v-if="query.price_order === 2" />
				</view>
			</view>
			<scroll-view class="show-price-body" @scrolltolower="moreList()" :scroll-into-view="listScrollId"
				scroll-y="true">
				<view class="show-price-item" v-for="(item, index) in list" :key="index"
					:id="`item_${item.users_goods_id}`" @click="selectItem(item)">
					<view class="left">
						<view>
							<image class="img" :src="item.image"></image>
						</view>
					</view>
					<view class="right">
						<view class="title">
							{{item.author_name}}
						</view>
						<view class="item-id">
							链上标识：<text>{{item.users_goods_id}}</text>
						</view>
						<view class="item-price">
							<text class="gold">￥{{item.price}}</text>
							<view class="item-price-right">
								<text>寄售</text>
								<!-- <text v-if="item.no_pay_status" class="collect-pay-status">
		            支付中
		          </text> -->
								<text v-if="item.status == 3" class="collect-pay-status">
									锁单中
								</text>
							</view>
						</view>
					</view>
					<image class="right-icon" src="../../static/back-icon.png" mode=""></image>
				</view>
			</scroll-view>
		</view>


	</view>
</template>
<script>
	export default {
		data() {
			return {
				btnDisabled: false,
				users_goods_id: '',
				info: {},

				showMore: false,
				query: {
					// categoryId: 0,
					goodsId: 0,
					search: '',
					time_order: '',
					price_order: 1,
					page: 1,
					pagesize: 20,
				},
				// 用户商品列表
				list: [],
				listLoading: false,
				listNoMore: false,
				// 全部编号列表 - 位置id
				listScrollId: '',
			}
		},
		onLoad(options) {
			this.users_goods_id = options.users_goods_id || 0;
			this.query.goodsId = options.goodsId || 0;
			if (this.query.goodsId > 0) {
				this.moreList();
			}
		},
		methods: {
			// 全部编号列表打开后，设置列表滚动的位置
			moreWindowOpen() {
				setTimeout(() => {
					this.listScrollId = 'item_' + this.users_goods_id;
				}, 30);
			},
			// 获取页面详情数据
			getDetail() {
				this.$http('api/trade/goodsDetail', {
					users_goods_id: this.users_goods_id,
				}).then(res => {
					this.info = res;
				}).catch((msg) => {

				});
			},
			// 选择编号
			selectItem(item) {
				if (item.status == 3) {
					return uni.showToast({
						icon: 'none',
						title: '该藏品锁定中',
					});
				}
				// 更新页面地址
				uni.redirectTo({
					url: `/pages/market/jsdetails?goodsId=${this.query.goodsId}&users_goods_id=${item.users_goods_id}`,
				});
				// 本页刷新，不更新地址
				// this.users_goods_id = item.users_goods_id;
				// this.getDetail();
				// this.showMore = false;
			},
			// 打开"全部编号"弹窗
			showMoreFn() {
				// if (this.list.length === 0) {
				//  this.moreList();
				// }
				this.query.page = 1;
				this.listNoMore = false;
				this.moreList();
				this.showMore = true;
			},
			moreListSortCheck(key) {
				if (this.listLoading) return;
				// '' 无排序 1正序 2倒叙
				let sortType = this.query[key];
				if (sortType) {
					sortType = sortType === 1 ? 2 : 1;
				} else {
					sortType = 1;
				}
				this.query.page = 1;
				this.query.price_order = '';
				this.query.time_order = '';
				this.query[key] = sortType;
				this.listNoMore = false;
				this.moreList();
			},
			// 全部编号列表请求
			moreList() {
				if (this.listNoMore || this.listLoading) return;
				this.listLoading = true;
				uni.showLoading({
					mask: true,
				});
				this.$http('api/trade/tradeGoodsList', this.query).then(res => {
						let data = res.data;
						this.list = this.query.page === 1 ? data : [...this.list, ...data];
						if (this.query.page === 1 && !this.info.users_goods_id) {
							if (this.users_goods_id > 0) {
								this.getDetail();
							} else if (data.length > 0) {
								this.users_goods_id = data[0].users_goods_id;
								this.getDetail();
							} else {
								uni.showModal({
									title: '提示',
									content: '该藏品已退市',
									confirmText: '返回',
									showCancel: false,
									success: () => {
										uni.navigateBack({
											delta: 1,
										});
									},
								});
							}
						}
						if (data.length < this.query.pagesize) {
							this.listNoMore = true;
						} else {
							this.query.page += 1;
						}
					})
					.catch(() => this.listNoMore = true)
					.finally(() => {
						this.listLoading = false;
						uni.hideLoading();
					});
			},
			handleBack() {
				if (getCurrentPages().length === 1) {
					uni.switchTab({
						url: '/pages/home/index',
					})
				} else {
					uni.navigateBack()
				}
			},
			toBuy() {
				this.btnDisabled = true
				this.$http('api/orders/tradeGoods', {
					users_goods_id: this.users_goods_id,
				}).then(res => {
					this.btnDisabled = false
					uni.navigateTo({
						url: '../pay/collectPay?order_id=' + res.order_id,
					})
				})
			},
			// 复制
			copyAddress(val) {
				// #ifdef APP-PLUS
				uni.setClipboardData({
					data: val,
					success: function() {
						uni.hideToast()
						uni.getClipboardData({
							success: function(res) {
								uni.showToast({
									title: '复制成功',
									icon: 'none',
								})
							},
						})
					},
				})
				// #endif
				// #ifdef H5
				let that = this
				this.$copyText(val).then(
					function(e) {
						uni.showToast({
							title: '复制成功!',
							icon: 'none',
						})
					},
					function(e) {
						uni.showToast({
							title: '复制失败!',
							icon: 'none',
						})
					}
				)
				// #endif
			},
		},
	}
</script>

<style lang="less">
	.show-price-body .uni-scroll-view-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-wrap: wrap;
	}
</style>
<style lang="less" scoped>
	page {
		background: #f5f5f5;
	}

	.white {
		color: #3c3c3c;
	}

	.gold {
		color: #9c8b66;
	}

	.back {
		position: relative;
		height: 0;

		image {
			position: absolute;
			width: 20rpx;
			height: 36rpx;
			top: 30rpx;
			left: 30rpx;
			z-index: 99;
		}
	}

	.collectImg {
		width: 100%;
		height: 450rpx;
	}

	.userImg {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
	}

	.buyBtn {
		color: #FFFFFF;
		width: 240rpx;
		height: 78rpx;
		font-size: 30rpx;
		line-height: 78rpx;
		margin: 0;
		padding: 0;
		text-align: center;
		background: linear-gradient(90deg, #EEA634, #DE2D32);
		border-radius: 39rpx;

		&::after {
			border: none;
		}
	}

	.copyImg {
		width: 24rpx;
		height: 24rpx;
		filter: brightness(0.5);
	}

	.btmWrap {
		position: fixed;
		bottom: 0;
		width: 100%;
		z-index: 10;
		background: #fff;
		height: 118rpx;

		.button {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 68rpx;
			border-radius: 34rpx;
			font-size: 24rpx;
			padding: 0 34rpx;
			line-height: 34rpx;
		}
	}

	// .collectImg {
	//   width: 342rpx;
	//   display: block;
	//   margin: 0 auto 340rpx;
	//   height: 323rpx;
	// }

	.tipsRight {
		background: #fff;
	}

	.topWrap {
		// padding-top: 180rpx;
		// background: url(../../static/detailsBg-2.png) 0 0 no-repeat;
		// background-size: 100% 100%;
		// width: 600rpx;
		// margin: 0 auto -70rpx;
		// height: 404rpx;
	}

	.infoWrap {
		/deep/ img {
			width: 100% !important;
			height: auto;
		}
	}

	.more {
		font-size: 28rpx;
		color: #9c8b66;
		display: flex;
		align-items: center;
		margin: 0;
		border: none;
		background: none;
		filter: brightness(0.1);

		&::after {
			display: none;
		}

		image {
			width: 24rpx;
			height: 24rpx;
			margin-left: 5rpx;
			// filter: brightness(0.6);
		}
	}



	.show-price-box {
		background-color: #f5f5f5;
		z-index: 100;
		overflow: hidden;

		.show-price-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 20rpx;
			height: 100rpx;
			background-color: #fff;

			text {
				font-size: 40rpx;
				font-weight: bold;
				color: #3c3c3c;
			}

			image {
				width: 30rpx;
				height: 30rpx;
			}
		}

		.sort-type {
			display: flex;
			background-color: #fff;
			height: 80rpx;
			padding: 10rpx 20rpx;

			button {
				border: none;
				border-radius: 0;
				line-height: 50rpx;
				height: 50rpx;
				padding: 0 30rpx;
				font-size: 28rpx;
				background: none;
				color: #666666;
				font-weight: bold;
			}

			button.active {
				background: #ffffff;
				color: #000000;
			}

			button::after {
				display: none;
			}
		}

		.sort {
			display: flex;
			// align-items: center;
			margin-left: 10rpx;
			justify-content: flex-start;

			image {
				width: 16rpx;
				height: 24rpx;
				margin-left: 10rpx;
			}

			&+.sort {
				margin-left: 40rpx;
			}
		}

		.show-price-body {
			width: 100%;
			height: calc(100vh - 400rpx);
			box-sizing: border-box;
			padding: 0 40rpx;
			background-color: #f5f5f5;

			.show-price-item {
				display: flex;
				align-items: center;
				padding: 20rpx 16rpx;
				margin: 20rpx 0;
				background-color: #fff;
				border-radius: 10rpx;

				.img {
					width: 180rpx;
					height: 180rpx;
					border-radius: 8rpx 0 0 8rpx;
				}

				.title {
					font-size: 30rpx;
					font-weight: bold;
					color: #1a1a1a;
					display: flex;
					align-items: center;
				}

				.item-id {
					font-size: 24rpx;

					text {
						font-weight: bold;
					}
				}

				.right {
					flex: 1;
					height: 180rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					margin-left: 20rpx;
					position: relative;

					.item-price {
						font-size: 32rpx;
						display: flex;
						justify-content: space-between;

						&>text:first-child {
							font-size: 34rpx;
							font-weight: bold;
						}

						.item-price-right {
							text {
								padding: 5rpx 10rpx;
								font-size: 24rpx;
								border-radius: 5rpx;
								margin-left: 10rpx;
								background-color: rgba(255, 16, 241, 0.1);
								color: rgba(209, 13, 200, 1.0);
								font-weight: bold;
							}

							.collect-pay-status {
								background-color: rgba(255, 16, 241, 0.1);
								color: rgba(209, 13, 200, 1.0);
							}
						}
					}
				}

				.item-price {
					font-size: 34rpx;
				}

				.right-icon {
					margin-left: auto;
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
	}
</style>
